package com.petRescueCenter.demo.pet.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.petRescueCenter.demo.pet.dao.PetDao;
import com.petRescueCenter.demo.pet.entity.Pet;
import com.petRescueCenter.demo.pet.entity.dto.PetDTO;

@Service
@Transactional
public class PetService implements IPetService {
	@Autowired
	private PetDao petDao;

	@Override
	public Pet save(Pet entity) {
		// TODO Auto-generated method stub
		return petDao.save(entity);
	}

	@Override
	public Pet findById(Long id) {
		// TODO Auto-generated method stub
		return petDao.findById(id).get();
	}

	@Override
	public boolean existsById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void deleteById(Long id) {
		// TODO Auto-generated method stub
		petDao.deleteById(id);
	}

	@Override
	public void deleteAll(Long[] ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Page<PetDTO> findAll(Specification<Pet> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Pet> entityLists = petDao.findAll(spec, pageable);
		List<PetDTO> dtoLists = null;
		if (entityLists != null) {
			dtoLists = new ArrayList<PetDTO>();
			for (Pet entity : entityLists) {
				PetDTO dto = new PetDTO();
				PetDTO.entityToDto(entity, dto);
				dtoLists.add(dto);
			}
		}
		return new PageImpl<PetDTO>(dtoLists, pageable, entityLists.getTotalElements());

	}

	@Override
	public boolean existsByName(String name) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Pet findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<String> getImg() {
		// TODO Auto-generated method stub
		return petDao.getImg();
	}

}
